package hojadetrabajo3;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author usuario
 */
public class bubble_sort {
        
    public static void swap(Comparable[] data, int i, int j)
    // pre: 0 <= i,j < data.length
    // post: data[i] and data[j] are exchanged
    {
        Comparable temp;
        temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }
    
    public static void bubbleSort(Comparable[] data)
    // pre: 0 <= n <= data.length
    // post: values in data[0..n-1] in ascending order
    {
        int n=data.length;
        int numSorted = 0; // number of values in order
        int index; // general index
        while (numSorted < n)
        {
            // bubble a large element to higher array index
            for (index = 1; index < n-numSorted; index++)
            {
                if (data[index-1].compareTo(data[index]) > 0)
                    swap(data,index-1,index);
            }
            // at least one more value in place
            numSorted++;
        }
    }   
}
